

// JavaScript Document
function menu(num){
	for(var i=1;i<=3;i++){
		if(i==num){
			document.getElementById("a"+i).style.display="block";
			document.getElementById("b"+i).className="b";
		}
		else{
			document.getElementById("a"+i).style.display="none";
			document.getElementById("b"+i).className="";
		}
	}
}

// JavaScript Document
function nTabs(thisObj,Num){
	if(thisObj.className == "active")return;
	var tabObj = thisObj.parentNode.id;
	var tabList = document.getElementById(tabObj).getElementsByTagName("li");
	for(i=0; i <tabList.length; i++)
	{
		if (i == Num)
		{
			thisObj.className = "active";
			document.getElementById(tabObj+"_Content"+i).style.display = "block";
		}else{
			tabList[i].className = "normal";
			document.getElementById(tabObj+"_Content"+i).style.display = "none";
		}
	}
}

$(function(){

	$("#lilist").hover(function(){
		$("#dllist").css("display","block");
	},function(){
		$("#dllist").css("display","none");
	});


	$(".addmain_index").click(function(){
		var orghtml = $("#add_index");
		var newhtml = $("#waitmain").html();
		$(newhtml).appendTo($(orghtml));
	});

	$(".delete_index").live("click",function(){
		var num = $(".right .delete_index").size();
		var main = $(this).parents('.maindls_index');
		if(num>1){
			main.remove();
		}else{
			alert('只有一个不能删除');
		}
	});

	$("#favo").click(function(){
		if(window.confirm('确认加入收藏?')){
			return true;
		}else{
			return false;
		}
	});

	$(".deleterecipe").click(function(){
		if(window.confirm('确认删除?')){
			return true;
		}else{
			return false;
		}
	});

	$(".lia").each(function(index){
		var a = $(this);
		if(a.attr('href')==SELF){
			a.css('background-image','url('+IMGS+'/four_hover.jpg)');
			a.css('background-repeat','no-repeat');
			return false;
		}
	});

	$("#foodsearch").live('focus',function(){
		var names = $(this);
		$("body").removeClass(".query");
		names.addClass(".query");
		var options = {
			//	serviceUrl:APP+'/Recipe/getmains',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/getmainss',//获取数据的后台页面
			width: 300,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){

				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{
					var splits = data.split('|');
					names.parents(".maindls_index").find("[name='mainfoodsectionid_index[]']").val(splits[0]);
					names.parents(".maindls_index").find("[name='mainfoodcategoryid_index[]']").val(splits[1]);
					names.parents(".maindls_index").find("[name='mainfoodarticleid_index[]']").val(splits[2]);
					names.css("background-color","white");
				}

				//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".maindls_index").find("[name='maindeal_index[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
					names.parents(".maindls_index").find("[name='mainamount_index[]']").val(1);
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").removeAttr("disabled");
				});
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("#mainsearch").live('focus',function(){
		var names = $(this);
		$("body").removeClass(".query");
		names.addClass(".query");
		var options = {
			//	serviceUrl:APP+'/Recipe/getmains',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/getmainss',//获取数据的后台页面
			width: 258,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){

				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{
					var splits = data.split('|');
					names.parents(".maindls_index").find("[name='mainfoodsectionid_index[]']").val(splits[0]);
					names.parents(".maindls_index").find("[name='mainfoodcategoryid_index[]']").val(splits[1]);
					names.parents(".maindls_index").find("[name='mainfoodarticleid_index[]']").val(splits[2]);
					names.css("background-color","white");
				}

				//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".maindls_index").find("[name='maindeal_index[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
					names.parents(".maindls_index").find("[name='mainamount_index[]']").val(1);
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").removeAttr("disabled");
				});
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("[name='main_index[]']").live('focus',function(){
		var names = $(this);
		$(".calcu").removeClass(".query");
		names.addClass(".query");
		var options = {
			//	serviceUrl:APP+'/Recipe/getmains',//获取数据的后台页面
			serviceUrl:APP+'/JsonData/getmainss',//获取数据的后台页面
			width: 120,//提示框的宽度
			delimiter: /(,|;)\s*/,//分隔符
//			onSelect: onAutocompleteSelect,//选中之后的回调函数
			onSelect: function(value, data){

				if(data==0){
					names.val('');
					names.css("background-color","yellow");
				}else{
					var splits = data.split('|');
					names.parents(".maindls_index").find("[name='mainfoodsectionid_index[]']").val(splits[0]);
					names.parents(".maindls_index").find("[name='mainfoodcategoryid_index[]']").val(splits[1]);
					names.parents(".maindls_index").find("[name='mainfoodarticleid_index[]']").val(splits[2]);
					names.css("background-color","white");
				}

				//	$.post(APP+'/Recipe/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
				$.post(APP+'/JsonData/getdeals', {sectionid:splits[0],categoryid:splits[1],articleid:splits[2]},function(datas){
					var obj = JSON.parse(datas);
					var org = names.parents(".maindls_index").find("[name='maindeal_index[]']");
					org.html('');
					for(var i=0; i<obj.length; i++){
						$('<option title="'+obj[i].deal+'g/'+obj[i]['setdeal']['name']+'" value="'+obj[i].unit+'">'+obj[i]['setdeal']['name']+'</option>').appendTo(org);
					}
					names.parents(".maindls_index").find("[name='mainamount_index[]']").val(1);
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainpreparation_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='mainamount_index[]']").removeAttr("disabled");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").css("background-color","white");
					names.parents(".maindls_index").find("[name='maindeal_index[]']").removeAttr("disabled");
				});
			},
			deferRequestBy: 0, //单位微秒
//			params: { country: 'Yes' },//参数
			noCache: false //是否启用缓存 默认是开启缓存的
//			lookup: ['January', 'Februarys','Februaryf','Februaryg','Februarys','Februaryg','Februaryg','Februarye','Februarye', 'March', 'April', 'May'] //local lookup values
		};
		names.autocomplete(options);
	});

	$("[name='main_index[]']").live('blur',function(){
		var name = $(this);
		if(name.val()==''){
			name.val('');
			name.css("background-color","yellow");
		}
	});

	$(".submit_index").toggle(function(){
		var firstamount = $("[name='mainamount_index[]']:first").val();
		var firstmain = $("[name='main_index[]']:first").val();
		if(firstmain=='' || firstamount=='' || firstamount==0){
			alert("原料不能为空");
			return false;
		}
		$(this).bind('click',getdatas());
		$(".calculate_index").css("display","none");
		//	$(".calculate ul").html('');
		$(".calculate_index").fadeIn();
	},function(){
		var firstamount = $("[name='mainamount_index[]']:first").val();
		var firstmain = $("[name='main_index[]']:first").val();
		if(firstmain=='' || firstamount=='' || firstamount==0){
			alert("原料不能为空");
			return false;
		}
		$(this).bind('click',getdatas());
		$(".calculate_index").css("display","none");
		//	$(".calculate ul").html('');
		$(".calculate_index").fadeIn();
	});


	function getdatas(){
		var mainamount = new Array();
		var maindeal = new Array();
		var mainsectionid = new Array();
		var maincategoryid = new Array();
		var mainarticleid = new Array();
		$("[name='mainamount_index[]']").each(function(index){
			var maina = $(this);
			mainamount[index] = maina.val();
		});
		$("[name='maindeal_index[]']").each(function(index){
			var maind = $(this);
			maindeal[index] = maind.val();
		});
		$("[name='mainfoodsectionid_index[]']").each(function(index){
			var mains = $(this);
			mainsectionid[index] = mains.val();
		});
		$("[name='mainfoodcategoryid_index[]']").each(function(index){
			var mainc = $(this);
			maincategoryid[index] = mainc.val();
		});
		$("[name='mainfoodarticleid_index[]']").each(function(index){
			var mainar = $(this);
			mainarticleid[index] = mainar.val();
		});

		$.post(APP+'/JsonData/calculates',{
			mainamount:mainamount,
			maindeal:maindeal,
			mainsectionid:mainsectionid,
			maincategoryid:maincategoryid,
			mainarticleid:mainarticleid
		},function(result){
			var obj = JSON.parse(result);
			$(".calculate_index ul li").each(function(index){
				var c=$(this);
				c.find(".spandata").html(obj[index]);
			});
//			$(".calculate ul").html(result);
		});
	}


	var osrc = IMGS+'/nav_';
	$("#nav li").each(function(index){
		var li = $(this);
		li.mouseover(function(){
			li.find("img").attr("src",osrc+(index+1)+'h.jpg');
		});

		li.mouseout(function(){
			li.find("img").attr("src",osrc+(index+1)+'.jpg');
		});
	});

	$("#clickcode").click(function(){
		this.src=this.src+'?id='+Math.random();
	});

	$(".onstart").each(function(index){
		var o = $(this);
		o.toggle(function(){
			switch(index){
				case 0:
					$(".onstart:lt(1)").attr("src",IMGS+'/five.gif');
					$("#onlevel").val(1);
					$(".onstart").addClass("onstart");
					break;
				case 1:
					$(".onstart:lt(2)").attr("src",IMGS+'/five.gif');
					$("#onlevel").val(2);
					$(".onstart").addClass("onstart");
					break;
				case 2:
					$(".onstart:lt(3)").attr("src",IMGS+'/five.gif');
					$("#onlevel").val(3);
					$(".onstart)").addClass("onstart");
					break;
				case 3:
					$(".onstart:lt(4)").attr("src",IMGS+'/five.gif');
					$("#onlevel").val(4);
					$(".onstart").addClass("onstart");
					break;
				case 4:
					$(".onstart:lt(5)").attr("src",IMGS+'/five.gif');
					$("#onlevel").val(5);
					$(".onstart").addClass("onstart");
					break;
			}
		},function(){
			switch(index){
				case 0:
					$(".onstart:gt(0)").attr("src",IMGS+'/rating_x.jpg');
					$("#onlevel").val(1);
					$(".onstart").addClass("onstart");
					break;
				case 1:
					$(".onstart:gt(1)").attr("src",IMGS+'/rating_x.jpg');
					$("#onlevel").val(2);
					$(".onstart").addClass("onstart");
					break;
				case 2:
					$(".onstart:gt(2)").attr("src",IMGS+'/rating_x.jpg');
					$("#onlevel").val(3);
					$(".onstart").addClass("onstart");
					break;
				case 3:
					$(".onstart:gt(3)").attr("src",IMGS+'/rating_x.jpg');
					$("#onlevel").val(4);
					$(".onstart").addClass("onstart");
					break;
				case 4:
					$(".onstart:gt(4)").attr("src",IMGS+'/rating_x.jpg');
					$("#onlevel").val(5);
					$(".onstart").addClass("onstart");
					break;
			}
		});
	});


	$("#userjoin").submit(function(){

		var username = $("[name=username]").val().length;
		var password = $("[name=password]").val().length;
		var cpassword = $("[name=cpassword]").val().length;
		var email = $("[name=email]").val().length;
		var emailv = $("[name=email]").val();
		var code = $("[name=code]").val().length;
		var check = $("[name=check]").attr("checked");

		if(username<1 || username>50){
			alert("Username Can't For Empty");
			return false;
		}

		if(username<6){
			alert("Username Can't less than six");
			return false;
		}

		if(password<1 || password>50){
			alert("Password Can't For Empty");
			return false;
		}

		if(password<6){
			alert("Password Can't less than six");
			return false;
		}

		if(cpassword<1 || cpassword>50){
			alert("rePassword Can't For Empty");
			return false;
		}

		if(cpassword<6){
			alert("rePassword Can't less than six");
			return false;
		}

		if($("[name=password]").val()!=$("[name=cpassword]").val()){
			alert("两次密码不一致");
		}

		if(email<1 || email>50){
			alert("Email Can't For Empty");
			return false;
		}

		if(!/^[\w\d\-\.]+@[\w\d\-\.]+\.[\w]+$/.test(emailv)){
			alert("Email Format Is Not Correct");
			emailv.val('');
			emailv.focus();
			return false;
		}

		if(code<1 || code>1000){
			alert("Code Can't For Empty");
			return false;
		}

		if(code>5){
			alert("Code Can't more than five");
			return false;
		}

		if(check!='checked'){
			alert("未同意本站协议,不能注册");
			return false;
		}

	});

	$("#login").submit(function(){

		var email = $("[name=email]").val().length;
		var emailv = $("[name=email]").val();
		var password = $("[name=password]").val().length;

		if(email<1 || email>50){
			alert("Email Can't For Empty");
			return false;
		}

		if(!/^[\w\d\-\.]+@[\w\d\-\.]+\.[\w]+$/.test(emailv)){
			alert("Email Format Is Not Correct");
			return false;
		}

		if(password<1 || password>50){
			alert("Password Can't For Empty");
			return false;
		}

		if(password<6){
			alert("rePassword Can't less than six");
			return false;
		}

	});

	$("#updateuserinfo").submit(function(){

		var nickname = $("[name=nickname]").val().length;
		var password = $("[name=password]").val();
		var cpassword = $("[name=cpassword]").val();

		if(nickname<1 || nickname>50){
			alert("Nickname Can't For Empty");
			return false;
		}

		if(nickname<5 || nickname>50){
			alert("Nickname Can't less than five");
			return false;
		}

		if(password!='' || cpassword!=''){

			if(password!=cpassword){
				alert("两次输入密码不一致");
				return false;
			}else if(password.length<6 || cpassword.length<6){
				alert('密码不能小于6位');
				return false;
			}
		}


	});

	$("#forgot").submit(function(){
		var email = $("[name=email]").val().length;
		var emailv = $("[name=email]").val();

		if(email<1 || email>50){
			alert("Email Can't For Empty");
			return false;
		}

		if(!/^[\w\d\-\.]+@[\w\d\-\.]+\.[\w]+$/.test(emailv)){
			alert("Email Format Is Not Correct");
			return false;
		}

	});

	$("#change").submit(function(){
		var password = $("[name=password]").val();
		var cpassword = $("[name=cpassword]").val();

		if(password!='' || cpassword!=''){
			if(password!=cpassword){
				alert("两次输入密码不一致");
				return false;
			}else if(password.length<6 || cpassword.length<6){
				alert('密码不能小于6位');
				return false;
			}
		}else{
			alert('密码不能为空');
			return false;
		}

	});


	$("#comments").submit(function(){

		var content = $("[name=content]");

		var code = $("[name=code]");

		if(content.val()==''){
			alert("Content Can't For Empty");
			return false;
		}

		if(code.attr("name")!='undefined'){
			if(code.val()==''){
				alert("Code Can't For Empty");
				return false;
			}else if(code.val().length<4){
				alert("Code Can't more than four");
				return false;
			}
		}

		if(content.val().length>500){
			alert("Content Can't more than 500 chars");
			return false;
		}

	});

	$("#codes").keyup(function(){
		if(this.value!=this.value.toUpperCase()){
			this.value=this.value.toUpperCase();
		}
	});

});

